package com.heu.blood.transfusion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.dict.service.DictDataService;
import com.heu.blood.transfusion.entity.BloodAdverseReactionEntity;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.transfusion.dao.PatientFeesDao;
import com.heu.blood.transfusion.entity.PatientFeesEntity;
import com.heu.blood.transfusion.service.PatientFeesService;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Service("patientFeesService")
public class PatientFeesServiceImpl extends ServiceImpl<PatientFeesDao, PatientFeesEntity> implements PatientFeesService {

    @Autowired
    private DictDataService dictDataService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 构造PatientFeesEntity对象
        PatientFeesEntity patientFees = new PatientFeesEntity(); // 用于接收传递过来的PatientFeesEntity参数
        if (Strings.isNotBlank((String) params.get("patientId"))){
            patientFees.setPatientId((String) params.get("patientId")); // 添加patientId
        }
        if (Strings.isNotBlank((String) params.get("payment"))){
            patientFees.setPayment((String) params.get("payment")); // 添加payment
        }
        if (Strings.isNotBlank((String) params.get("department"))){
            patientFees.setDepartment((String) params.get("department")); // 添加department
        }
        if (Strings.isNotBlank((String) params.get("ward"))){
            patientFees.setWard((String) params.get("ward")); // 添加ward
        }
        if (Strings.isNotBlank((String) params.get("executiveDivision"))){
            patientFees.setExecutiveDivision((String) params.get("executiveDivision")); // 添加executiveDivision
        }
        if (Strings.isNotBlank((String) params.get("patientType"))){
            patientFees.setPatientType((String) params.get("patientType")); // 添加executiveDivision
        }
        if (Strings.isNotBlank((String) params.get("chargeType"))){
            patientFees.setChargeType((String) params.get("chargeType")); // 添加executiveDivision
        }
        if (Strings.isNotBlank((String) params.get("name"))){
            patientFees.setName((String) params.get("name")); // 添加executiveDivision
        }



        // 时间格式化，解决Date->String 报错
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = (String)params.get("startTime");
        String endTime = (String) params.get("endTime");


        // 构造条件构造器对象
        LambdaQueryWrapper<PatientFeesEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.eq(Strings.isNotBlank(patientFees.getPatientId()), PatientFeesEntity::getPatientId, patientFees.getPatientId())
                .eq(Strings.isNotBlank(patientFees.getPayment()), PatientFeesEntity::getPayment, patientFees.getPayment())
                .eq(Strings.isNotBlank(patientFees.getName()), PatientFeesEntity::getName, patientFees.getName())
                .eq(Strings.isNotBlank(patientFees.getDepartment()), PatientFeesEntity::getDepartment, patientFees.getDepartment())
                .eq(Strings.isNotBlank(patientFees.getWard()), PatientFeesEntity::getWard, patientFees.getWard())
                .eq(Strings.isNotBlank(patientFees.getExecutiveDivision()), PatientFeesEntity::getExecutiveDivision, patientFees.getExecutiveDivision())
                .eq(Strings.isNotBlank(patientFees.getPatientType()), PatientFeesEntity::getPatientType, patientFees.getPatientType())
                .eq(Strings.isNotBlank(patientFees.getChargeType()), PatientFeesEntity::getChargeType, patientFees.getChargeType())
                .eq(Strings.isNotBlank(String.valueOf(patientFees.getIfShow())), PatientFeesEntity::getIfShow,IF_SHOW.code())
                .ge(Strings.isNotBlank(startTime), PatientFeesEntity::getPricingDate, startTime)
                .le(Strings.isNotBlank(endTime),PatientFeesEntity::getPricingDate, endTime);

        // 根据不良反应时间升序排列
        queryWrapper.orderByAsc(PatientFeesEntity::getPricingDate);
        IPage<PatientFeesEntity> page = this.page(
                new Query<PatientFeesEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 提交收费项目
     * @param patientFees
     * @return
     */
    @Override
    public String savePatientFees(PatientFeesEntity patientFees) {

        String code = patientFees.getPayment();
        patientFees.setUnitPrice(dictDataService.queryByCode(code).getDictAppendix());
        Double unitPrice= Double.valueOf((dictDataService.queryByCode(code).getDictAppendix()).split("\\D+")[0]);
        Double quantity = Double.valueOf(patientFees.getQuantity());
        patientFees.setProjectAmount(String.format("%.2f",quantity*unitPrice));
        this.save(patientFees);

        return "保存成功";
    }

}